package ysq.xyg.recovery.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.recovery.dao.RecoverySackDetailDao;
import ysq.xyg.recovery.model.po.RecoverySackDetail;
import ysq.xyg.recovery.model.vo.RecoverySackDetailVo;
import ysq.xyg.recovery.service.RecoverySackDetailService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-袋子详情表service实现
 *
 * @author congcong tao
 * @date 2019-7-3 20:51:03
 */
@Service("recoverySackDetailService")
public class RecoverySackDetailServiceImpl extends BaseServiceImpl<RecoverySackDetail> implements RecoverySackDetailService {

    @Resource
    private RecoverySackDetailDao recoverySackDetailDao;

    private RecoverySackDetail recoverySackDetail;

    @Autowired
    public void setBaseDao(RecoverySackDetailDao recoverySackDetailDao) {
        this.baseDao = recoverySackDetailDao;
    }

    /**
     * 分页查询
     *
     * @param recoverySackDetailVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-7-3 20:51:03
     */
    @Override
    public ReturnEntity page(RecoverySackDetailVo recoverySackDetailVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoverySackDetail> list = acceptList(recoverySackDetailVo);
        PageInfo<RecoverySackDetail> page=new PageInfo<RecoverySackDetail>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoverySackDetailVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-7-3 20:51:03
     */
    @Override
    public ReturnEntity addOrUpdate(RecoverySackDetailVo recoverySackDetailVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoverySackDetailVo.getId())){
            recoverySackDetail=new RecoverySackDetail(currentUser);
            isUpate=false;
        }else{
            recoverySackDetail=selectByPrimaryKey(recoverySackDetailVo.getId());
            if(ObjectUtil.isNull(recoverySackDetail)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoverySackDetail.setUpdateTime(new Date());
            recoverySackDetail.setUpdateUser(currentUser.getId());
        }
        recoverySackDetail.setRecordId(recoverySackDetailVo.getRecordId());
        recoverySackDetail.setCatId(recoverySackDetailVo.getCatId());
        recoverySackDetail.setCatName(recoverySackDetailVo.getCatName());
        recoverySackDetail.setCatPrice(recoverySackDetailVo.getCatPrice());
        recoverySackDetail.setCatNum(recoverySackDetailVo.getCatNum());
        String allPrice = "0";
        if (StringUtils.isNotEmpty(recoverySackDetailVo.getCatPrice()) && StringUtils.isNotEmpty(recoverySackDetailVo.getCatNum())) {
            String catPrice = recoverySackDetailVo.getCatPrice();
            String catNum = recoverySackDetailVo.getCatNum();
            allPrice = String.valueOf(Double.parseDouble(catPrice) * Integer.parseInt(catNum));
            recoverySackDetail.setAllPrice(allPrice);
        }
        recoverySackDetail.setIncomePrice(recoverySackDetailVo.getIncomePrice());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(recoverySackDetail);
        } else {
            num = insert(recoverySackDetail);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-7-3 20:51:03
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoverySackDetail recoverySackDetail=selectByPrimaryKey(id);
        if(recoverySackDetail!=null){
            recoverySackDetail.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoverySackDetail.setUpdateTime(new Date());
            recoverySackDetail.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoverySackDetail);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoverySackDetailVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-7-3 20:51:03
     */
    @Override
    public List<RecoverySackDetail> acceptList(RecoverySackDetailVo recoverySackDetailVo){
        Example example = new Example(RecoverySackDetail.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoverySackDetailVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoverySackDetailVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(recoverySackDetailVo.getRecordId())){
            criteria.andEqualTo("recordId",recoverySackDetailVo.getRecordId());
        }
        if(StringUtil.isNotEmpty(recoverySackDetailVo.getCatName())){
            criteria.andEqualTo("catName",recoverySackDetailVo.getCatName());
        }
        example.setOrderByClause(" update_time desc");
        return recoverySackDetailDao.selectByExample(example);
    }
}
